package com.example.campuslink.ui.activity

import android.text.Editable
import android.text.TextWatcher
import android.view.View
import android.widget.AdapterView
import android.widget.ArrayAdapter
import androidx.recyclerview.widget.LinearLayoutManager
import com.example.campuslink.BR
import com.example.campuslink.R
import com.example.campuslink.commen.adpter.FansListAdapter
import com.example.campuslink.commen.adpter.HaveBtnUserInfoAdapter
import com.example.campuslink.commen.enums.TimeEnum
import com.example.campuslink.commen.tools.Sp
import com.example.campuslink.commen.tools.SpacesItemButtonDecoration
import com.example.campuslink.commen.tools.putExtraKey
import com.example.campuslink.databinding.ActivityFansAndAttentionBinding
import com.example.campuslink.viewModel.FansAndAttentionViewModel

class FansAndAttentionActivity :
    BaseActivity<ActivityFansAndAttentionBinding, FansAndAttentionViewModel>(
        FansAndAttentionViewModel::class.java,
        R.layout.activity_fans_and_attention,
        BR.viewModel
    ) {
    private var rvAdapter: HaveBtnUserInfoAdapter? = null
    private var fansAdapter: FansListAdapter? = null
    private val time = TimeEnum.getAllLabels()
    private var nowNumber = ""
    override fun initView() {
        initData()
//        val isFirstJump = intent.getBooleanExtra(putExtraKey.IS_FIRST_JUMP.key, false)
        val adapter =
            ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, time)
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
        mViewDataBinding?.spinnerShowTime?.setAdapter(adapter)
        mViewModel?.userList?.observe(this) {
            if (mViewModel?.type?.value == putExtraKey.FANS.key) {
                mViewDataBinding?.rvUserList?.adapter = fansAdapter
                if (mViewModel?.sortTime?.value == TimeEnum.RECENT_TIME.label) {
                    fansAdapter?.setListData(it?.reversed() ?: listOf())
                } else {
                    fansAdapter?.setListData(it ?: listOf())
                }
            } else {
                mViewDataBinding?.rvUserList?.adapter = rvAdapter
                if (mViewModel?.sortTime?.value == TimeEnum.RECENT_TIME.label) {
                    rvAdapter?.setListData(it?.reversed() ?: listOf())
                } else {
                    rvAdapter?.setListData(it ?: listOf())
                }
            }
        }
        mViewModel?.sortTime?.observe(this) {
            if (mViewModel?.type?.value == putExtraKey.FANS.key) {
                mViewModel?.getFansList(nowNumber)
            } else {
                mViewModel?.getAttentionList(nowNumber)
            }
        }
        mViewDataBinding?.rvUserList?.adapter = rvAdapter
        mViewDataBinding?.rvUserList?.layoutManager = LinearLayoutManager(this)
        mViewDataBinding?.rvUserList?.addItemDecoration(SpacesItemButtonDecoration(30))
        onClick()
    }

    private fun onClick() {
        mViewDataBinding?.ivBack?.setOnClickListener({
            finish()
        })
        mViewDataBinding?.etSearchContent?.addTextChangedListener(object : TextWatcher {
            override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {

            }

            override fun onTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {

            }

            override fun afterTextChanged(p0: Editable?) {
                mViewModel?.findUserInfo(p0.toString(), nowNumber)
            }

        })
        mViewDataBinding?.rgRadioGroup?.setOnCheckedChangeListener({ group, checkedId ->
            when (checkedId) {
                R.id.rb_fans -> {
                    getFansPager()
                }

                R.id.rb_attention -> {
                    getAttentionPager()
                }

            }
        })
        mViewDataBinding?.spinnerShowTime?.setOnItemSelectedListener(object :
            AdapterView.OnItemSelectedListener {
            override fun onItemSelected(p0: AdapterView<*>?, p1: View?, p2: Int, p3: Long) {
                mViewModel?.setSortTime(time[p2])
            }

            override fun onNothingSelected(p0: AdapterView<*>?) {

            }
        })
        mViewDataBinding?.freshLayout?.setOnRefreshListener {
            if (mViewModel?.type?.value == putExtraKey.FANS.key) {
                mViewModel?.getFansList(nowNumber)
            } else {
                mViewModel?.getAttentionList(nowNumber)
            }
            mViewDataBinding?.freshLayout?.isRefreshing = false
        }
    }

    private fun getFansPager() {
        val number = intent.getStringExtra(putExtraKey.USER_NUMBER.key) ?: Sp.getString(
            this,
            putExtraKey.USER_NUMBER.key,
            ""
        )
        mViewModel?.setType(putExtraKey.FANS.key)
        mViewModel?.getAttentionListAndFansList(nowNumber)
        mViewDataBinding?.rbFans?.isChecked = true
        mViewDataBinding?.rbAttention?.isChecked = false
        mViewDataBinding?.etSearchContent?.setText("")
        mViewDataBinding?.tvType?.text = "我的粉丝: ${mViewModel?.fansList?.value?.size ?: 0}"
        mViewModel?.getFansList(nowNumber)
    }

    private fun getAttentionPager() {
        val number = intent.getStringExtra(putExtraKey.USER_NUMBER.key) ?: Sp.getString(
            this,
            putExtraKey.USER_NUMBER.key,
            ""
        )
        mViewModel?.setType(putExtraKey.ATTENTION.key)
        mViewDataBinding?.rbFans?.isChecked = false
        mViewModel?.getAttentionListAndFansList(nowNumber)
        mViewDataBinding?.etSearchContent?.setText("")
        mViewDataBinding?.rbAttention?.isChecked = true
        mViewDataBinding?.tvType?.text = "我的关注: ${mViewModel?.attentionList?.value?.size ?: 0}"
        mViewModel?.getAttentionList(nowNumber)
    }

    private fun initData() {
        mViewModel?.context = this
        mViewModel?.initDB(this)

        val formType = intent.getStringExtra(putExtraKey.JUMP_TYPE.key) ?: ""
        if (formType == putExtraKey.FANS.key) {
            getFansPager()
        } else if (formType == putExtraKey.ATTENTION.key) {
            getAttentionPager()
        }
        nowNumber = intent.getStringExtra(putExtraKey.USER_NUMBER.key) ?: ""
        mViewModel?.setType(formType)
        mViewModel?.getAttentionListAndFansList(nowNumber)
        fansAdapter = FansListAdapter(mViewModel!!, nowNumber)
        rvAdapter = HaveBtnUserInfoAdapter(mViewModel!!, nowNumber = nowNumber)
        mViewModel?.jumpNumberString = nowNumber
    }
}